package com.weijianhuawen.wjblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weijianhuawen.wjblog.common.domain.dos.CommentDo;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-12-08 22:32
 */


public interface CommentMapper extends BaseMapper<CommentDo> {
    // 根据评论路由地址，评论状态搜索评论
    default List<CommentDo> selectByRouterUrlAndStatus(String routerUrl, Integer status) {
        return selectList(Wrappers.<CommentDo>lambdaQuery()
                .eq(CommentDo::getRouterUrl, routerUrl)
                .eq(CommentDo::getStatus, status)
                .orderByDesc(CommentDo::getCreateTime));
    }

    /**
     * 分页查询
     * @param current
     * @param size
     * @param startDate
     * @param endDate
     * @return
     */
    default Page<CommentDo> selectPageList(Long current, Long size, String routerUrl,
                                           LocalDate startDate, LocalDate endDate, Integer status) {
        // 分页对象(查询第几页、每页多少数据)
        Page<CommentDo> page = new Page<>(current, size);

        // 构建查询条件
        LambdaQueryWrapper<CommentDo> wrapper = Wrappers.<CommentDo>lambdaQuery()
                .like(StringUtils.isNotBlank(routerUrl), CommentDo::getRouterUrl, routerUrl) // like 模糊查询
                .eq(Objects.nonNull(status), CommentDo::getStatus, status) // 评论状态
                .ge(Objects.nonNull(startDate), CommentDo::getCreateTime, startDate) // 大于等于 startDate
                .le(Objects.nonNull(endDate), CommentDo::getCreateTime, endDate)  // 小于等于 endDate
                .orderByDesc(CommentDo::getCreateTime); // 按创建时间倒叙

        return selectPage(page, wrapper);
    }

    /**
     * 根据 reply_comment_id 查询评论
     * @param replyCommentId
     * @return
     */
    default List<CommentDo> selectByReplyCommentId(Long replyCommentId) {
        return selectList(Wrappers.<CommentDo>lambdaQuery()
                .eq(CommentDo::getReplyCommentId, replyCommentId)
                .orderByDesc(CommentDo::getCreateTime)
        );
    }

    /**
     * 根据 parent_comment_id 删除
     * @param id
     * @return
     */
    default int deleteByParentCommentId(Long id) {
        return delete(Wrappers.<CommentDo>lambdaQuery()
                .eq(CommentDo::getParentCommentId, id));
    }

    // 根据用户id获取评论列表
    default List<CommentDo> selectCommentListByUserId(Long userId) {
        return selectList(Wrappers.<CommentDo>lambdaQuery()
                .eq(Objects.nonNull(userId), CommentDo::getUserId, userId));
    }
}
